package com.eerl.pdev.terminal.cmd;

import com.eerl.pdev.util.EToolPluginConsoleToolWindowFactory;
import com.eerl.pdev.util.Notice;
import com.eerl.pdev.util.ProjectFileUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

import java.nio.file.Path;
import java.nio.file.Paths;

public class TerminalActionMakeThis extends AnAction {

    public TerminalActionMakeThis() {
        super("Erl Make This Mod", "", null);
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        // xll_todo 提取成为配置项
        final String PROJECT_WINDOWS_ABS_PATH = "D:\\git\\workspace\\cahx_wspace\\develop\\server\\src\\mod";
        final String PROJECT_WSL_ABS_PATH = "/root/git/workspace/cahx_wspace/develop";

        // 获取当前项目
        Project project = e.getProject();
        if (project == null) {
            throw new RuntimeException("exec [TerminalActionMakeThis] project is null");
        }

        // 获取当前打开的文件
        VirtualFile virtualFile = e.getData(CommonDataKeys.VIRTUAL_FILE);
        if (virtualFile == null) {
            Notice.popMessage(e, "No file is currently open");
            return;
        }

        // 获取文件路径
        String filePath = virtualFile.getPath();
        EToolPluginConsoleToolWindowFactory.printToConsole(project, filePath);
        // 获取文件所在目录的路径
        // String directoryPath = virtualFile.getParent().getPath();
        Path relativizePath = Paths.get(PROJECT_WINDOWS_ABS_PATH).relativize(Paths.get(filePath));
        Terminal instance = Terminal.getInstance(project);
        ProjectFileUtil.saveFile(e);
        String command = "cd " + PROJECT_WSL_ABS_PATH + ";mmod " + relativizePath.getParent().toString().replace("\\", "/");
        instance.executeCommand(command);

    }
}
